use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions



set myEmacsclient to "~/Documents/Repositories/emacs/nextstep/Emacs.app/Contents/MacOS/bin/emacsclient"


# create file in Finder

tell application "Finder"

try

set myFolder to (the target of the front Finder window) as alias

on error

beep

end try

set myFileName to name of myFolder

display dialog ¬

"New text file name:" default answer myFileName & ".org" buttons {"Cancel", "Create"} ¬

default button 2

set myFileName to text returned of result

if exists file myFileName of myFolder then

display alert ¬

"A file named ‘" & myFileName & "’ already exists in this folder." as informational

return

end if

set myPath to ((POSIX path of myFolder) & myFileName)

do shell script "touch " & quoted form of myPath

#open file in Emacs

display dialog ¬

"Open file with Emacs?" buttons {"Yes", "Cancel"} ¬

default button 1

if button returned of result is "Yes" then

try

tell application "System Events" to tell process "Emacs" to set frontmost to true

on error

tell application "/Users/suzume/Documents/Code/emacs/nextstep/Emacs.app"

activate

delay 1

end tell

end try

set selectedFile to myPath

set myCommand to myEmacsclient & " -n -e \"(find-file " & "\\\"" & selectedFile & "\\\"" & ")\""

do shell script myCommand

end if

end tell